﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;
using Component.Worker.RecursiveProcess;
public partial class AdminX_Product_ProductMain : System.Web.UI.UserControl
{
    #region Constants
    private DataTable mCateData = new DataTable();
    protected const string HOLIZOTAL_COLUMN_NAME = "HolizotalSort";
    protected const string VERTICAL_COLUMN_NAME = "VerticalSort";

    #endregion Constants
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            this.ShowMess();
            this.SetLabelLanguage();
            this.BindDataToLanguageDropDown();
            this.BindDataToDropCategory();
            this.BindDataToGrid(1);
        }
    }

    #region Bind DropdownList
    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
        ddlLanguage.SelectedValue = ConvertData.ConvertToString(Support.GetDefaultLanguage());
    }
    private void BindDataToDropCategory()
    {

        int nDefaultLang = Support.GetDefaultLanguage();
        int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        ddlCategory.Items.Clear();
        DataTable objTable = new DataTable();
        ProductCategory objProductCategory = new ProductCategory();
        objTable = objProductCategory.Search();
        int nCount = objTable.Rows.Count;
        FlowCategory flow = new FlowCategory(objTable, VERTICAL_COLUMN_NAME, HOLIZOTAL_COLUMN_NAME);
        flow.IDColumnName = "ProductCategoryID";
        flow.ParentIDColumName = "[Parent]";

        switch (nCurrentLang)
        {
            case 1:
                flow.SortColumnName = "ProductCategoryName1";
                break;
            case 2:
                flow.SortColumnName = "ProductCategoryName2";
                break;
            case 3:
                flow.SortColumnName = "ProductCategoryName3";
                break;
        }
        flow.ParentSortDirection = "ASC";
        if (flow.Execute(0))
        {
            mCateData = flow.DestinationData();
        }
        if (mCateData != null)
        {
            foreach (DataRow dr in mCateData.Rows)
            {
                if (dr != null)
                {
                    string sText = string.Empty;
                    switch (nCurrentLang)
                    {
                        case 1:
                            sText = Convert.ToString(dr["ProductCategoryName1"]);
                            break;
                        case 2:
                            sText = Convert.ToString(dr["ProductCategoryName2"]);
                            break;
                        case 3:
                            sText = Convert.ToString(dr["ProductCategoryName3"]);
                            break;

                    }
                    Int16 iHolz = Convert.ToInt16(dr[HOLIZOTAL_COLUMN_NAME]);
                    textCreateMarginLeft(ref sText, iHolz);
                    ListItem item = new ListItem(sText, Convert.ToString(dr["ProductCategoryID"]));
                    ddlCategory.Items.Add(item);
                }
            }
            Utl.SetNonSelectToControl(ddlCategory, MultipleLanguage.AllTextItem[nCurrentLang - 1], "0");
        }
        else
        {
            ddlCategory.DataSource = mCateData;

            switch (nCurrentLang)
            {
                case 1:
                    ddlCategory.DataTextField = "ProductCategoryName";
                    break;
                case 2:
                    ddlCategory.DataTextField = "ProductCategoryName";
                    break;
                case 3:
                    ddlCategory.DataTextField = "ProductCategoryName";
                    break;
            }
        }
        ddlCategory.DataValueField = "ProductCategoryID";
        ddlCategory.DataBind();
    }

    private void textCreateMarginLeft(ref string text, Int16 count)
    {
        if (count > 0)
        {
            string sMargin = String.Empty;
            for (int i = 0; i < count; i++)
            {
                sMargin += "--";
                sMargin += " ";
            }
            text = sMargin + text;
        }
    }

    
    #endregion

    #region  Message
   
    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        btnAddnew.Text = MultipleLanguage.BTN_ADDNEW[nDefaultLanguage - 1];
        btnUpdate.Text = MultipleLanguage.UPDATE[nDefaultLanguage - 1];
        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaultLanguage - 1];
        lblCategory.Text = MultipleLanguage.PRODUCTCAT[nDefaultLanguage - 1];
        
    }

    private void ShowMess()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        switch (nMessage)
        {
            case 1:
                MessageBoxss.Show(MessagesAlert.INSERT_SUCCESSFULL[nDefaultLanguage]);
                break;
            case 2:
                MessageBoxss.Show(MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage]);

                break;
            case 3:
                MessageBoxss.Show(MessagesAlert.ITEM_NOTEXIST[nDefaultLanguage]);
                break;
            default:
                
                break;
        }
    }
    #endregion

    #region Repeater Process

    private void BindDataToGrid(int CurrentPageIndex)
    {

        int nCategory = ConvertData.ConvertToInt(ddlCategory.SelectedValue);

        ProductCategory objProductCategory = new ProductCategory();
        DataTable dtbProductCategory = new DataTable();
        dtbProductCategory = objProductCategory.SearchByParent(nCategory);
        int nCount = dtbProductCategory.Rows.Count;
        string sParent = "";
        if (nCategory != -1)
        {
            sParent = ConvertData.ConvertToString(nCategory);
        }
        if (nCount > 0)
        {

            for (int i = 0; i < nCount; i++)
            {
                sParent = sParent + "," + ConvertData.ConvertToString(dtbProductCategory.Rows[i]["ProductCategoryID"]) + ",";

                ProductCategory objProductCategory2 = new ProductCategory();
                DataTable dtbProductCategory2 = new DataTable();
                dtbProductCategory2 = objProductCategory2.SearchByParent(ConvertData.ConvertToInt(dtbProductCategory.Rows[i]["ProductCategoryID"]));
                int nCount2 = dtbProductCategory2.Rows.Count;
                if (nCount2 > 0)
                {

                    for (int y = 0; y < nCount2; y++)
                    {
                        sParent = sParent + "," + ConvertData.ConvertToString(dtbProductCategory2.Rows[y]["ProductCategoryID"]) + ",";

                        ProductCategory objProductCategory3 = new ProductCategory();
                        DataTable dtbProductCategory3 = new DataTable();
                        dtbProductCategory3 = objProductCategory3.SearchByParent(ConvertData.ConvertToInt(dtbProductCategory2.Rows[y]["ProductCategoryID"]));
                        int nCount3 = dtbProductCategory3.Rows.Count;
                        if (nCount3 > 0)
                        {
                            for (int z = 0; z < nCount3; z++)
                            {
                                sParent = sParent + "," + ConvertData.ConvertToString(dtbProductCategory3.Rows[z]["ProductCategoryID"]) + ",";

                                ProductCategory objProductCategory4 = new ProductCategory();
                                DataTable dtbProductCategory4 = new DataTable();
                                dtbProductCategory4 = objProductCategory4.SearchByParent(ConvertData.ConvertToInt(dtbProductCategory3.Rows[z]["ProductCategoryID"]));
                                int nCount4 = dtbProductCategory4.Rows.Count;
                                if (nCount4 > 0)
                                {
                                    for (int z1 = 0; z1 < nCount4; z1++)
                                    {
                                        sParent = sParent + "," + ConvertData.ConvertToString(dtbProductCategory4.Rows[z1]["ProductCategoryID"]) + ",";
                                    }
                                }
                            }
                        }
                    }
                }
            }
            sParent = sParent.Replace(",,", ",");
            sParent = ConvertData.TruncateString(sParent, sParent.Length - 1);
        }

        Products objProduct = new Products();
        DataTable dtb = new DataTable();

        int nTrademark = -1;
        int nMaterial = -1;
        double dPriceFrom = 0;
        double dPriceTo = 100000000;
        string sProductName = "";
        int nStatus = -1;
        int nPageCount = 0;
        int nItemView = 0;
        int nItemID = 0;
        int isSaleOff = -1;
        int PercentSaleOff = 0;
        int ExpriedDateSaleOff = 0;
        int StartedDateSaleOff = 0;
        int BestSeller = -1;
        int nPageSize = Config.PAGESIZE_MAX;
        int nDefaultLang = Support.GetDefaultLanguage();
        int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);

        dtb = objProduct.Search(nItemView, "", sProductName, nItemID, sParent, nTrademark, nMaterial, dPriceFrom, dPriceTo, isSaleOff, BestSeller, nStatus, PercentSaleOff, ExpriedDateSaleOff, StartedDateSaleOff, nDefaultLang, nCurrentLang, 1, nPageSize, ref nPageCount);
        rpProduct.DataSource = dtb;
        rpProduct.DataBind();
        lblTotalRecord.Text = MultipleLanguage.LBL_TOTAL_ITEM[nDefaultLang - 1] + nPageCount + MultipleLanguage.LBL_FOUND[nDefaultLang - 1];

    }

    protected void rpNews_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);
        Products objProduct = new Products();
        switch (strCommand)
        {
            case "Delete":
                objProduct.LoadById(nID);
                Support.DeleteFile("product", objProduct.Data.Image1);
                Support.DeleteFile("product", objProduct.Data.Image2);
                Support.DeleteFile("product", objProduct.Data.Image3);
                Support.DeleteFile("product", objProduct.Data.Image4);
                Support.DeleteFile("product", objProduct.Data.ImageDefault);
                Support.DeleteFile("file", objProduct.Data.UserGuide);
                int nDelete = objProduct.DeleteById(nID);
             
                BindDataToGrid(1);
                break;

            case "Edit":
               
                string sEdit = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_PRODUCT_ADD + "&" + Config.ACTION + "=" + Config.ACTION_EDIT + "&" + Config.ACTION_ID + "=" + nID;
                Response.Redirect(sEdit);
                break;
            case "Active":
                int nActive = objProduct.UpdateStatus(nID, EnumeType.INACTIVE);
                
                BindDataToGrid(1);
                break;

            case "Inactive":
                int nInactive = objProduct.UpdateStatus(nID, EnumeType.ACTIVE);
                
                BindDataToGrid(1);
                break;
        }
    }
    protected void rpNews_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblProductCode = (Label)e.Item.FindControl("lblProductCode");
            Label lblProductName = (Label)e.Item.FindControl("lblProductName");

            Label lblCategory = (Label)e.Item.FindControl("lblCategory");
            Label lblTradeMark = (Label)e.Item.FindControl("lblTradeMark");

            TextBox txtPriority = (TextBox)e.Item.FindControl("txtPriority");
            Label lblMaterial = (Label)e.Item.FindControl("lblMaterial");
            Label lblPrice = (Label)e.Item.FindControl("lblPrice");

            LinkButton lnkActive = (LinkButton)e.Item.FindControl("lnkActive");
            LinkButton lnkInactive = (LinkButton)e.Item.FindControl("lnkInactive");
            LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");

            Label lblProductID = (Label)e.Item.FindControl("lblProductID");
            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "ProductID"));

            lblProductID.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductID"));
            lblProductCode.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCode"));
            lblProductName.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductName"));
            lblCategory.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCategoryName"));
            txtPriority.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Priority"));

           

            lnkEdit.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_ALERT[nDefaultLanguage]);
            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductID"));
        }
    }
   


    #endregion

    #region Events

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.BindDataToGrid(1);
    }

    protected void btnAddnew_Click1(object sender, EventArgs e)
    {
        string sAddNew = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_PRODUCT_ADD + "&" + Config.ACTION + "=" + Config.ACTION_ADD;
        Response.Redirect(sAddNew);
    }



    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.BindDataToDropCategory();
        this.BindDataToGrid(1);
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        int nCount = rpProduct.Items.Count;
        int nDefaultLanguage = Support.GetDefaultLanguage();
        for (int i = 0; i < nCount; i++)
        {
            TextBox txtPriority = (TextBox)rpProduct.Items[i].FindControl("txtPriority");
            Label lblProductID = (Label)rpProduct.Items[i].FindControl("lblProductID");

            int nProductID = ConvertData.ConvertToInt(lblProductID.Text);
            int nPriority = ConvertData.ConvertToInt(txtPriority.Text);

            Products objProducts = new Products();
            int nResult = objProducts.UpdatePriority(nProductID, nPriority);
        }
        this.BindDataToGrid(1);
    }
    #endregion


   
}